package com.mxsoft.concurrent;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CyclicBarrierDemo {

    public static void main(String[] args) throws InterruptedException {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(2, new Runnable() {
            @Override
            public void run() {
                System.out.println("我是最后执行的");
            }
        });

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(Thread.currentThread().getName()+" start evaluate");

                    Thread.sleep(3000);
                    System.out.println(Thread.currentThread().getName()+" end evaluate");

                    cyclicBarrier.await();


                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    System.out.println(Thread.currentThread().getName()+" start evaluate");
                    Thread.sleep(10000);
                    System.out.println(Thread.currentThread().getName()+" end evaluate");

                    cyclicBarrier.await();

                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        Thread.sleep(15000);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(Thread.currentThread().getName()+" start evaluate");

                    Thread.sleep(3000);
                    System.out.println(Thread.currentThread().getName()+" end evaluate");

                    cyclicBarrier.await();


                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    System.out.println(Thread.currentThread().getName()+" start evaluate");
                    Thread.sleep(10000);
                    System.out.println(Thread.currentThread().getName()+" end evaluate");

                    cyclicBarrier.await();

                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        System.out.println(Thread.currentThread().getName()+"  end  ");
    }
}
